package com.bee.beemanage.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bee.beemanage.api.entity.SysDepartment;
import com.bee.beemanage.api.entity.SysUser;
import com.bee.beemanage.api.service.SysDepartmentService;
import com.bee.beemanage.system.common.utils.context.CurrentUser;
import com.bee.beemanage.system.common.utils.pages.PageUtils;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/sys-department")
@Api(value ="部门表",tags = "部门表")
public class SysDepartmentController {

    @Resource
    private SysDepartmentService sysDepartmentService;

    /**
    * 新增部门表
    * @param sysDepartment
    * @return
    */
    @ApiOperation(value = "新增部门表", notes = "新增部门表")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:department:save')")
    public Result save(@RequestBody SysDepartment sysDepartment) {
        SysUser loginUser = CurrentUser.getLoginUser();
        sysDepartment.setCreateTime(LocalDateTime.now());
        sysDepartment.setCreateUser(loginUser.getId());
        sysDepartment.setCreateUser(loginUser.getId());
        return success(sysDepartmentService.save(sysDepartment));
    }
    /**
    * 修改部门表
    * @param sysDepartment
    * @return
    */
    @ApiOperation(value = "修改部门表", notes = "修改部门表")
    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('sys:department:edit')")
    public Result edit(@RequestBody SysDepartment sysDepartment) {
        SysUser loginUser = CurrentUser.getLoginUser();
        sysDepartment.setUpdateTime(LocalDateTime.now());
        sysDepartment.setUpdateUser(loginUser.getId());
        sysDepartment.setUpdateUser(loginUser.getId());
        return success(sysDepartmentService.updateById(sysDepartment));
    }
    /**
    * 根据id删除部门表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id删除部门表", notes = "通过id删除部门表")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:department:removeById')")
    public Result removeById(@PathVariable String id) {
        return sysDepartmentService.safeDeleteById(id);
    }
    /**
    * 根据id批量删除部门表
    * @param ids
    * @return
    */
    @ApiOperation(value = "通过id批量删除部门表", notes = "通过id批量删除部门表")
    @PostMapping("/batch")
    @PreAuthorize("hasAnyAuthority('sys:department:removeByIds')")
    public Result removeByIds(@RequestBody String[] ids) {
    List<String> idsList = Arrays.asList(ids);
        return success(sysDepartmentService.removeByIds(idsList));
    }
    /**
     * 部门表分页查询
     * @param current     当前页
     * @param size        一页的条数
     * @param orderColumn 排序字段
     * @param orderAsc    升降序
     * @param sysDepartment  查询对象
     * @param startTime 创建起始时间
     * @param endTime 创建终止时间
     * @return 带分页信息的部门列表
     */
    @ApiOperation(value = "部门表分页查询", notes = "部门表分页查询")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:department:page')")
    public Result page(@RequestParam(defaultValue = "0") long current, @RequestParam(defaultValue = "10") long size,
                       String orderColumn, Boolean orderAsc, SysDepartment sysDepartment,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Result result;
        Page<SysDepartment> page = new Page<>();
        PageUtils.start(current, size, orderColumn, orderAsc, page);
        if (sysDepartment.getDeleted()) {
            result = success(sysDepartmentService.recycleSearch(page, sysDepartment, startTime, endTime));
        } else {
            QueryWrapper<SysDepartment> query = Wrappers.query(sysDepartment);
            if (null != startTime && null != endTime) {
                query.between("create_time", startTime, endTime);
            }
            result = success(sysDepartmentService.page(page, query));
        }
        return result;
    }

    /**
    * 根据id查询部门表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id查询部门表", notes = "通过id查询部门表")
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:department:getById')")
    public Result getById(@PathVariable("id") String id) {
        return success(sysDepartmentService.getById(id));
    }

    /**
     * 数据瘦身，物理删除所有逻辑删除的数据（真删）
     * @return 删除是否成功
     */
    @ApiOperation(value = "物理删除所有逻辑删除的数据", notes = "物理删除所有逻辑删除的数据")
    @DeleteMapping("/clear")
    @PreAuthorize("hasAnyAuthority('sys:department:clear')")
    public Result clear() {
        return sysDepartmentService.clear();
    }

    /**
     * 根据id恢复逻辑删除的数据
     * @return 恢复是否成功
     */
    @ApiOperation(value = "根据id恢复逻辑删除的数据", notes = "根据id恢复逻辑删除的数据")
    @PostMapping("/recover")
    @PreAuthorize("hasAnyAuthority('sys:department:recover')")
    public Result recover(@RequestBody String[] ids) {
        List<String> idsList = Arrays.asList(ids);
        return success(sysDepartmentService.recover(idsList));
    }
}